package com.imti.service.impl;

import com.imti.mapper.ExamMapper;
import com.imti.model.*;
import com.imti.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
public class ExamServiceImpl implements ExamService {

    // 注入RedisTemplate
    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ExamMapper examMapper;

    public ExamServiceImpl(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void findTrueAnswer(String subPart, HttpServletRequest request) {

    }

    /**
     * 提交成绩
     * @param exmVo
     * @return
     */
    @Transactional
    public boolean subPap(ExmVo exmVo) {
        try {
            int score = 0;
            //3-1#4-0#  单选 判断
            String pid = exmVo.getpId();
            Paper p = examMapper.getPaperInfo(pid);
            String radioids = exmVo.getQuestionIds();
            String[] radids = radioids.split("#");
            for (int i = 0; i < radids.length; i++) {//3-1
                String qid = radids[i].split("-")[0];
                String answid = radids[i].split("-")[1];
                //获取问题答案
                Question question = examMapper.getQuestionByQId(qid);
                Record r = new Record();
                r.setUserId(1);
                r.setPaperId(Integer.parseInt(pid));
                r.setQuestionId(Integer.parseInt(qid));
                r.setAnsweredId(answid);
                if (question.getAnswerId().equals(answid)) {
                    r.setCorrectFlag("1");//答案正确 //1 单选  2 多选  3 判断
                    if ("1".equals(question.getType())) {//单选
                        score += p.getRadioScore();
                    } else if ("3".equals(question.getType())) {//判断
                        score += p.getJudgeScore();
                    }
                } else {
                    r.setCorrectFlag("0");
                }
                //保存考试记录信息
                examMapper.saveRecode(r);
            }
            //3-1,2,3,4,#4-3,2,#  多选
            String checkids = exmVo.getAnswerIds();
            String[] checks = checkids.split("#");
            for (int i = 0; i < checks.length; i++) {//3-1,2,3,4,
                String qid = checks[i].split("-")[0];
                String answid = checks[i].split("-")[1];
                answid = answid.substring(0, answid.length() - 1);
                Record r = new Record();
                //获取问题答案
                Question question = examMapper.getQuestionByQId(qid);
                Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
                r.setUserId(userId);
                r.setPaperId(Integer.parseInt(pid));
                r.setQuestionId(Integer.parseInt(qid));
                r.setAnsweredId(answid);
                if (question.getAnswerId().equals(answid)) {
                    r.setCorrectFlag("1");//答案正确 //1 单选  2 多选  3 判断
                    score += p.getCheckScore();
                } else {
                    r.setCorrectFlag("0");
                }
                //保存考试记录信息
                examMapper.saveRecode(r);
            }
            //保存成绩
            ExamScore es = new ExamScore();
            Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
            es.setUserId(userId);
            es.setExamId(Integer.parseInt(pid));
            es.setTotalScore(score);
            examMapper.saveExmScore(es);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
